﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 社团
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void hide()
        {
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            groupBox3.Visible = false;
            groupBox4.Visible = false;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sid = textBox1.Text;
            string str = comboBox1.Text;
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                switch (str)
                {
                    case "社团":
                        var query1 = "select * from 社团信息表 where cno=@sid";
                        using (SqlCommand command = new SqlCommand(query1, connection))
                        {
                            command.Parameters.AddWithValue("@sid", sid);
                            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                            {
                                DataTable dataTable = new DataTable();
                                adapter.Fill(dataTable);
                                dataGridView1.DataSource = dataTable;
                            }
                        }
                        break;
                    case "成员":
                        var query2 = "select *from 社团成员表 where sno=@sid";
                        using (SqlCommand command = new SqlCommand(query2, connection))
                        {
                            command.Parameters.AddWithValue("@sid", sid);
                            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                            {
                                DataTable dataTable = new DataTable();
                                adapter.Fill(dataTable);
                                dataGridView1.DataSource = dataTable;
                            }
                        }
                        break;
                    case "活动":
                        var query3 = "select *from 社团活动表 where Acno=@sid";
                        using (SqlCommand command = new SqlCommand(query3, connection))
                        {
                            command.Parameters.AddWithValue("@sid", sid);
                            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                            {
                                DataTable dataTable = new DataTable();
                                adapter.Fill(dataTable);
                                dataGridView1.DataSource = dataTable;
                            }
                        }
                        break;
                    case "用户":
                        var query4 = "select *from 用户表 where userid=@sid";
                        using (SqlCommand command = new SqlCommand(query4, connection))
                        {
                            command.Parameters.AddWithValue("@sid", sid);
                            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                            {
                                DataTable dataTable = new DataTable();
                                adapter.Fill(dataTable);
                                dataGridView1.DataSource = dataTable;
                            }
                        }
                        break;
                }

            }
        }


        private void 社团信息管理ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            hide();
            groupBox1.Visible = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string cno = textBox2.Text;
            string cname = textBox3.Text;
            DateTime ctime = dateTimePicker1.Value;
            string location = textBox4.Text;
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                var query = "update 社团信息表 set cname=@cname,ctime=@ctime,clocation=@location where cno=@cno";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@cno", cno);
                    command.Parameters.AddWithValue("@cname", cname);
                    command.Parameters.AddWithValue("@ctime", ctime);
                    command.Parameters.AddWithValue("@location", location);
                    command.ExecuteNonQuery();
                    MessageBox.Show("更新了社团信息表的数据");
                }
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void 社员信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox2.Visible = true;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            hide();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sno = textBox5.Text;
            string sname = textBox6.Text;
            string ssex = textBox7.Text;
            string sage = textBox8.Text;
            string stelp = textBox9.Text;
            string pid = comboBox2.Text;
            switch (pid)
            {
                case "社长":
                    pid = "1";
                    break;
                case "副社长":
                    pid = "2";
                    break;
                case "部长":
                    pid = "3";
                    break;
                case "副部长":
                    pid = "4";
                    break;
                case "社员":
                    pid = "5";
                    break;

            }
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                var query = "UPDATE 社团成员表 SET sname = @sname, ssex = @ssex, sage = @sage, stelp = @stelp, pid = @pid WHERE sno = @sno";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    // 添加参数
                    command.Parameters.AddWithValue("@sno", sno);
                    command.Parameters.AddWithValue("@sname", sname);
                    command.Parameters.AddWithValue("@ssex", ssex);
                    command.Parameters.AddWithValue("@sage", sage);
                    command.Parameters.AddWithValue("@stelp", stelp);
                    command.Parameters.AddWithValue("@pid", pid);
                    command.ExecuteNonQuery();
                    MessageBox.Show("更新了社团成员表数据");
                }
            }

        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void 社团活动管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox3.Visible = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string Acno = textBox10.Text;
            string Acname = textBox11.Text;
            DateTime Actime = dateTimePicker2.Value;
            string Aclocation = textBox12.Text;
            string Acfund = textBox13.Text;
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                var query = "UPDATE 社团活动表 SET Acname = @acname, Actime = @actime, Aclocation = @aclocation, Acfund = @acfund WHERE Acno = @acno";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    // 添加参数
                    command.Parameters.AddWithValue("@acno", Acno);
                    command.Parameters.AddWithValue("@acname", Acname);
                    command.Parameters.AddWithValue("@actime", Actime);
                    command.Parameters.AddWithValue("@aclocation", Aclocation);
                    command.Parameters.AddWithValue("@acfund", Acfund);

                    // 执行更新操作
                    int rowsAffected = command.ExecuteNonQuery();
                    MessageBox.Show($"更新了 {rowsAffected} 行。");
                }
            }
        }

        private void 账号设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hide();
            groupBox4.Visible = true;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string userid = textBox14.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                var query = "delete from 用户表 where userid=@userid";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@userid", userid);
                    int rowsAffected = command.ExecuteNonQuery();
                    MessageBox.Show($"删除了 {rowsAffected} 行。");
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string userid = textBox14.Text;
            string username = textBox15.Text;
            string password = textBox16.Text;
            string power = comboBox3.Text;
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                var query = "INSERT INTO 用户表 (userid, username, password, power) VALUES (@userid, @username, @password, @power)";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@userid", userid);
                    command.Parameters.AddWithValue("@username", username);
                    command.Parameters.AddWithValue("@password", password);
                    command.Parameters.AddWithValue("@power", power);
                    int rowsAffected = command.ExecuteNonQuery();
                    MessageBox.Show($"添加了{rowsAffected}行数据");
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string userid = textBox14.Text;
            string username = textBox15.Text;
            string password = textBox16.Text;
            string power = comboBox3.Text;
            sqlcon sqlcon = new sqlcon();
            using (SqlConnection connection = sqlcon.connect1())
            {
                var query = "update 用户表 set username=@username,password=@password,power=@power where userid=@userid";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@userid", userid);
                    command.Parameters.AddWithValue("@username", username);
                    command.Parameters.AddWithValue("@password", password);
                    command.Parameters.AddWithValue("@power", power);
                    int rowsAffected = command.ExecuteNonQuery();
                    MessageBox.Show($"修改了{rowsAffected}行数据");
                }
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            string cno = textBox17.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                var query = "delete from 社团信息表 where cno= @cno;";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@cno", cno);
                    command.ExecuteNonQuery();
                    MessageBox.Show("删除成功");
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string cno = textBox21.Text;
            string cname = textBox20.Text;
            DateTime ctime = dateTimePicker3.Value;
            string clocation = textBox19.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                string query = "insert into 社团信息表(cno,cname,ctime,clocation)values (@cno,@cname,@ctime,@clocation)";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@cno", cno);
                    command.Parameters.AddWithValue("@cname", cname);
                    command.Parameters.AddWithValue("@ctime", ctime);
                    command.Parameters.AddWithValue("@clocation", clocation);
                    command.ExecuteNonQuery();
                    MessageBox.Show("添加数据成功");
                }
            }

        }

        private void button11_Click(object sender, EventArgs e)
        {
            string sno = textBox22.Text;
            string sname = textBox23.Text;
            string ssex = textBox24.Text;
            string sage = textBox25.Text;
            string stelp = textBox26.Text;
            string pid = comboBox4.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                var query = "insert into 社团成员表 (sno,sname,ssex,sage,stelp,pid)values(@sno,@sname,@ssex,@sage,@stelp,@pid)";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@sno", sno);
                    command.Parameters.AddWithValue("@sname", sname);
                    command.Parameters.AddWithValue("@ssex", ssex);
                    command.Parameters.AddWithValue("@sage", sage);
                    command.Parameters.AddWithValue("@stelp", stelp);
                    command.Parameters.AddWithValue("@pid", pid);
                    command.ExecuteNonQuery();
                    MessageBox.Show("添加成功");
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string Acno = textBox27.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                string query = "delete from 社团活动表 where Acno=@Acno";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@Acno", Acno);
                    command.ExecuteNonQuery();
                    MessageBox.Show("删除成功");

                }
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string Acno = textBox31.Text;
            string Acname = textBox30.Text;
            DateTime Actime = dateTimePicker4.Value;
            string Aclocation = textBox29.Text;
            string Acfund = textBox28.Text;
            sqlcon sqlcon = new sqlcon();
            using(SqlConnection connection=sqlcon.connect1())
            {
                var query = "insert into 社团活动表(Acno,Acname,Actime,Aclocation,Acfund)values(@Acno,@Acname,@Actime,@Aclocation,@Acfund)";
                using(SqlCommand command=new SqlCommand(query,connection))
                {
                    command.Parameters.AddWithValue("@acno", Acno);
                    command.Parameters.AddWithValue("@acname", Acname);
                    command.Parameters.AddWithValue("@actime", Actime);
                    command.Parameters.AddWithValue("@aclocation", Aclocation);
                    command.Parameters.AddWithValue("@acfund", Acfund);
                    command.ExecuteNonQuery();
                    MessageBox.Show("添加成功");
                }
            }
        }
    }
}
